package com.ithei.date.mydate;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class 学习回顾2021年6月15日 {
    public static void main(String[] args) {
        //LocalDateTime创建方式
        LocalDateTime ldt1=LocalDateTime.now();
        LocalDateTime ldt2=LocalDateTime.of(2020,11,11,11,11,11);
        //获取字段信息
        int year = ldt1.getYear();
        int monthValue = ldt1.getMonthValue();
        int dayOfYear = ldt1.getDayOfYear();
        int dayOfYear1 = ldt1.getDayOfYear();
        DayOfWeek dayOfWeek = ldt1.getDayOfWeek();
        int value = dayOfWeek.getValue();
        int hour = ldt1.getHour();
        int minute = ldt1.getMinute();
        int second = ldt1.getSecond();
        LocalDate ld=ldt2.toLocalDate();
        LocalTime lt=ldt2.toLocalTime();
        //LocalDate和LocalTime获取字段省略了
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format = ldt2.format(dtf);
        System.out.println("格式化日期对象的字符串: ");
        System.out.println(format);
        LocalDateTime parse = LocalDateTime.parse(format, dtf);
        System.out.println("解析字符串的日期对象LocalDateTime:    ");
        System.out.println(parse);
        //增加字段的代码
        LocalDateTime localDateTime = ldt1.plusYears(1).plusMonths(1).plusDays(1).plusHours(1).plusMinutes(1).plusSeconds(1).plusWeeks(1);
        //LocalDateTime localDateTime1 = ldt1.minusYears(1);与plus方法相反
        //with方法
        LocalDateTime localDateTime1 = ldt1.withYear(2019);
        LocalDateTime localDateTime2 = ldt1.withMonth(12);
        Period period= Period.between(ldt2.toLocalDate(),ldt1.toLocalDate());
        int years = period.getYears();
        int months = period.getMonths();
        int days = period.getDays();
        //period.getTotalMonths()

        Duration duration=Duration.between(ldt1,ldt2);



    }
}
